﻿using DataAccess;
using DTO;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAO
{
    public class PhieuXuatDAO
    {
        public static List<PhieuXuatDTO> LoadDanhDanhSachPhieuXuat()
        {
            List<PhieuXuatDTO> list = new List<PhieuXuatDTO>();
            string query = "SELECT * FROM PHIEUXUAT ";
            DataTable dt = DataConnection.ExecQuery(query);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DataRow dr = dt.Rows[i];
                    PhieuXuatDTO dl = new PhieuXuatDTO();
                    dl.MaPhieuXuat = dr["MaPhieuXuat"].ToString();
                    dl.MaDaiLy = dr["MaDaiLy"].ToString();
                    dl.NgayLapPhieu = dr["NgayLapPhieu"].ToString();
                    dl.MaNhanVien = dr["MaNhanVien"].ToString();
                    dl.TongGiaTri = int.Parse(dr["TongGiaTri"].ToString()); 
                    list.Add(dl);
                }
            }
            return list;
        }


        public static bool deletePhieuXuat(string MaPhieuXuat)
        {
            string query = "DELETE PHIEUXUAT WHERE MaPhieuXuat = '" + MaPhieuXuat + "'";
            try
            {
                DataConnection.ExecNonQuery(query);
                return true;
            }
            catch (System.Exception e)
            {
                Console.WriteLine("Not access to database:{0}", e.Message);
                return false;
            }
        }

        public static string TaoMaPhieuXuat()
        {
            String strChu = "PX";
            String strSo = "0";
            String strMaDau = "PX001";

            String strSql = "SELECT TOP 1 MaPhieuXuat FROM PHIEUXUAT ORDER BY MaPhieuXuat DESC";

            try
            {
                String strMaCuoi = DataConnection.ExecScalar(strSql).ToString();
                String strTemp = ((int.Parse(strMaCuoi.Remove(0, 2)) + 1)).ToString();
                String strMaDaiLy = strChu;
                while (strMaDaiLy.Length + strTemp.Length < 5)
                    strMaDaiLy += strSo;
                return strMaDaiLy + strTemp;

            }
            catch (Exception)
            {
                return strMaDau;
            }
        }

        public static bool AddMaPhieuXuat(string MaPhieuXuat,string MaNhanVien)
        {
            string query = "INSERT INTO PHIEUXUAT (MaNhanVien,MaPhieuXuat,TongGiaTri) VALUES('" + MaNhanVien + "','" + MaPhieuXuat + "','0')";
            try
            {
                DataConnection.ExecQuery(query);
                return true;
            }
            catch (System.Exception e)
            {
                Console.WriteLine("Not access to database:{0}", e.Message);
                return false;
            }
        }

        public static bool CapNhatPhieuXuat(PhieuXuatDTO px)
        {
            string sql = "UPDATE PHIEUXUAT SET MaDaiLy='"+px.MaDaiLy+"',NgayLapPhieu='"+px.NgayLapPhieu+"',TongGiaTri='"+px.TongGiaTri+"',MaNhanVien='"+px.MaNhanVien+"' WHERE MaPhieuXuat='"+px.MaPhieuXuat+"'";
            try
            {
                DataConnection.ExecNonQuery(sql);
                return true;
            }
            catch (System.Exception e)
            {
                Console.WriteLine("Not access to database:{0}", e.Message);
                return false;
            }
        }

        public static bool deletePhieuXuat()
        {
            string query = "DELETE PHIEUXUAT WHERE TongGiaTri = '0'";
            try
            {
                DataConnection.ExecQuery(query);
                return true;
            }
            catch (System.Exception e)
            {
                Console.WriteLine("Not access to database:{0}", e.Message);
                return false;
            }
        }
    }
}
